<template >
    <div>
        <!-- 添加商品 -->
        <div class="mbx">
            <el-breadcrumb separator-class="el-icon-arrow-right">
                <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
                <el-breadcrumb-item>商品管理</el-breadcrumb-item>
                <el-breadcrumb-item>添加商品</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <!-- 卡片  -->
        <el-card>
            <!-- 消息提示 -->
            <el-alert title="消息提示的文案" type="info" show-icon>
            </el-alert>
            <!-- 步骤条 -->
            <el-steps :active="active - 0" finish-status="success" align-center>
                <el-step title="基本信息"></el-step>
                <el-step title="商品参数"></el-step>
                <el-step title="商品属性"></el-step>
                <el-step title="商品图片"></el-step>
                <el-step title="商品内容"></el-step>
                <el-step title="完成"></el-step>
            </el-steps>

            <!-- 标签页 -->
            <el-form :model="ruleForm" label-position="top" :rules="rules" ref="ruleForm" class="demo-ruleForm">
                <el-tabs @tab-click="tabclick" tab-position="left" :before-leave="fns" style="height: 100%;"
                    v-model="active">

                    <!-- 基本信息 -->
                    <el-tab-pane label="基本信息" name="0">
                        <el-form-item label="商品分类" prop="goods_cat">
                            <!-- 级联选择器 -->
                            <el-cascader v-model="ruleForm.goods_cat" :options="catlist" @change="handleChange"
                                :props="{ label: 'cat_name', value: 'cat_id' }"></el-cascader>
                        </el-form-item>
                        <el-form-item label="商品名称" prop="goods_name">
                            <el-input v-model="ruleForm.goods_name"></el-input>
                        </el-form-item>
                        <el-form-item label="商品价格" prop="goods_price">
                            <el-input v-model="ruleForm.goods_price"></el-input>
                        </el-form-item>
                        <el-form-item label="商品重量" prop="goods_weight">
                            <el-input v-model="ruleForm.goods_weight"></el-input>
                        </el-form-item>
                        <el-form-item label="商品数量" prop="goods_number">
                            <el-input v-model="ruleForm.goods_number"></el-input>
                        </el-form-item>



                    </el-tab-pane>
                    <!-- 动态数剧  -->
                    <el-tab-pane label="商品参数" name="1">
                        <!-- 商品参数 -->
                        <!-- <el-form-item :label="item1.attr_name" v-for="(item1, index1) in manylist" :key="item1.attr_id">
                            <el-checkbox-group v-model="item1.checked" size="small">
                                <el-checkbox :label="item2" border v-for="(item2, index2) in item1.attr_vals"
                                    :key="index2"></el-checkbox>

                            </el-checkbox-group>
                        </el-form-item> -->
                      <arrtslist :list="manylist"></arrtslist>
                     
                    </el-tab-pane>
                    <!-- 静态数剧 -->
                    <el-tab-pane label="商品属性" name="2">
                        <!-- 商品属性 -->
                        <!-- <el-form-item :label="item1.attr_name" v-for="(item1, index1) in onlylist" :key="item1.attr_id">
                            <el-checkbox-group v-model="item1.checked" size="small">
                                <el-checkbox checked :label="item2" border v-for="(item2, index2) in item1.attr_vals"
                                    :key="index2"></el-checkbox>

                            </el-checkbox-group>
                        </el-form-item> -->
                        <arrtslist :list="onlylist"></arrtslist>
                    </el-tab-pane>

                    <el-tab-pane label="商品图片" name="3">
                        <!-- 商品图片-->
                        <el-upload class="upload-demo" action="http://www.shidiqi.asia/api/private/v1/upload"
                            list-type="picture" :on-success="success" :headers="headers">
                            <el-button size="small" type="primary">点击上传</el-button>
                            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
                        </el-upload>

                    </el-tab-pane>

                    <el-tab-pane label="商品内容" name="4">
                        <!-- 富文本编辑器 -->
                        <quill-editor ref="VueQuillEditor" v-model="ruleForm.goods_introduce" />

                    </el-tab-pane>

                    <el-tab-pane label="完成" name="5">
                        <el-button type="primary" @click="submitForm('ruleForm')">完成</el-button>
                    </el-tab-pane>
                </el-tabs>
            </el-form>

        </el-card>
    </div>
</template>
<script>
//引入lodash 进行数据深拷贝
import _ from "lodash"
import arrtslist from "../components/arrtslist.vue"
import { getclassify, attrib, attribo, getaddshop } from "../api/api"
export default {
    name: "goodsadd",
    components:{arrtslist},
    data() {
        return {
            active: 0,//选中下标
            ruleForm: {
                goods_cat: '',
                goods_name: "",
                goods_price: "",
                goods_number: "",
                goods_weight: "",
                pics: [],
                goods_introduce: '',
                attrs: [],

            },//表单数据
            rules: {
                goods_name: { required: true, message: '请输入商品名称', trigger: 'blur' },
                goods_price: { required: true, message: '请输入用价格', trigger: 'blur' },
                goods_number: { required: true, message: '请输入用数量', trigger: 'blur' },
                goods_weight: { required: true, message: '请输入用重量', trigger: 'blur' },
                goods_cat: { required: true, message: '请输入用重量', trigger: 'blur' },
            },
            //级联选择器
            catlist: [],
            isjlid: [],
            //商品参数
            manylist: [],
            //商品属性
            onlylist: [],
            //图片上传设置的请求头
            headers: { Authorization: window.localStorage.getItem('token') },
            //富文本编辑器的内容
            content: '',
            //商品图片




        }
    },
    methods: {
        //分类数据
        classiy() {
            getclassify().then(res => {
                // console.log(res);
                this.catlist = res.data
                console.log(this.catlist);
            })
        },
        handleChange(val) {
            console.log(val);
            this.isjlid = val
        },
        //步骤条判断
        fns(to, from) {
            // console.log(newName, oldName);
            if (from == 0 && this.ruleForm.goods_cat.length < 3) {
                this.$message({
                    message: "请填写分类",
                    type: "error",
                });
                return false;
            }
        },

        //tab点击active事件
        tabclick() {
            //获取动态参数
            
            if (this.active == 1 && this.manylist.length == 0) {
                attrib({ id: this.ruleForm.goods_cat[2] }).then(res => {
                    // console.log(res);
                    //先循环数剧拿到每一项
                    res.data.forEach(item => {
                        //在以split字符串方法以空格分开
                        let attr = item.attr_vals.split(" ")
                        item.attr_vals = attr
                         item.checked=_.cloneDeep(attr)


                    });

                    this.manylist = res.data

                })
            }
            //获取静态参数
            if (this.active == 2 && this.onlylist.length == 0) {
                attribo({ id: this.ruleForm.goods_cat[2] }).then(res => {
                    console.log(res);
                    res.data.forEach(item => {
                        let attr = item.attr_vals.split(" ")
                        item.attr_vals = attr
                         item.checked=_.cloneDeep(attr)

                    });
                    this.onlylist = res.data

                })

            }

        },

        //上传图片成功事件
        success(res) {
            // console.log(res);
            this.ruleForm.pics.push({ pic: res.data.tmp_path })
        },

        //点击完成进行添加

        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    console.log('submit!');
                    let ruleForm = _.cloneDeep(this.ruleForm)
                    ruleForm.goods_cat = ruleForm.goods_cat.join(",")
                    let manylist = this.manylist.map(item => {
                        return {
                            attr_id: item.attr_id,
                            attr_value: item.checked.join(" ")
                        }
                    })
                    let onlylist = this.onlylist.map(item => {
                        return {
                            attr_id: item.attr_id,
                            attr_value: item.checked.join(" ")
                        }
                    })
                    ruleForm.attrs = manylist.push(...onlylist)

                    getaddshop(this.ruleForm).then(res => {
                        console.log(res);
                        this.$router.push('/goods')
                    })

                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },







    },

    created() {
        this.classiy()
    },

}
</script>
<style lang="scss" scoped>
.el-card {
    margin-top: 20px;

}

.el-steps {
    margin: 15px 0;
}

.shopclass {
    p {
        margin: 10px;
    }
}
</style>